module hong {
	/** 成语消消乐的主类 */
	export class xxlgame extends baseGame {
		/** 每个元素的宽带 */
		private cellWidth:number = 54;
		/** 每个元素的高带 */
		private cellHeight:number = 54;
		/** x轴需要的图标数量 */
		private Xnum:number = 8;
		/** y轴需要的图标数量 */
		private Ynum:number = 16;
		/** 显示元素列表 */
		private list:cellM[][] = null;
		/** 选中元素蒙版 */
		private selMask:egret.Shape;
		/** 选中的元素 */
		private selItem:cellM;

		public constructor() {
			super();
			this.RES = "xxl";
			this.list = [];
			
			this.selMask = new egret.Shape();
			this.selMask.graphics.beginFill(0xffffff,0.3);
			this.selMask.graphics.drawRect(0,0,54,54);
			this.selMask.graphics.endFill();
			this.selMask.visible = false;
			this.selItem = null;

			//this.touchEnabled = true;
			this.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onClick, this);
		}
		/** 游戏显示 */
		public gameShow():void{
			this.removeChildren();
			for(let i=0;i<this.Ynum;i++){
				this.list[i] = [];
				for(let j=0;j<this.Xnum;j++){
					let cm:cellM = new cellM();
					cm.texture = RES.getRes(((Math.random()*5+1)|0).toString());
					this.list[i][j] = cm;
					cm.x = j * this.cellWidth;
					cm.y = i * this.cellHeight;
					cm.touchEnabled = true;
					this.addChild(cm);
				}
			}
			this.addChild(this.selMask);
			super.gameShow();
		}
		/** 游戏的宽度 */
		public gWidth():number{
			return this.Xnum * this.cellWidth;
		}
		/** 游戏的高度 */
		public gHeight():number{
			return this.Ynum * this.cellHeight;
		}
		/** 点击了元素 */
		public onClick(e:egret.TouchEvent):void{
			//console.log(e.target);
			do{
				if(!this.selItem) break; //没有选择元素需要交换检查
				let cItem:cellM = e.target;
				if(this.selItem.texture == cItem.texture) return; //相同的元素不交换
				if(Math.abs(cItem.x - this.selItem.x) / this.cellWidth + Math.abs(cItem.y - this.selItem.y) / this.cellHeight > 1) break;
				let sx1 = this.selItem.x / this.cellWidth, sy1 = this.selItem.y / this.cellHeight, sx2 = cItem.x / this.cellWidth, sy2 = cItem.y / this.cellHeight;
				let dlist = [], dmap = {}, inl = 0;
				dmap[sx2+"_"+sy2] = 1;
				this.checkItem(dlist, dmap, this.selItem, cItem);
				while(inl < dlist.length){
					this.checkItem(dlist, dmap, dlist[inl], cItem);
					inl++;
				}
				if(dlist.length < 2){
					dmap = {};
					dlist = [];
					inl = 0;
				}else{
					dlist.push(cItem);
					inl++;
				}
				dmap[sx1+"_"+sy1] = 1;
				let cl = inl;
				this.checkItem(dlist, dmap, cItem, this.selItem);
				while(inl < dlist.length){
					this.checkItem(dlist, dmap, dlist[inl], this.selItem);
					inl++;
				}
				if(dlist.length - cl > 1){
					dlist.push(this.selItem);
				}else{
					dlist = dlist.slice(0, cl);
				}
				if(dlist.length > 1){
					this.list[sy2][sx2] = this.selItem;
					this.list[sy1][sx1] = cItem;
					egret.Tween.get(this.selItem).to({x:cItem.x, y:cItem.y}, 400);
					egret.Tween.get(cItem).to({x:this.selItem.x, y:this.selItem.y}, 500).call(()=>{
						//if(this.selItem.texture == dlist[0].texture){
							//dlist.push(this.selItem);
							//this.removeChild(this.selItem);
							//this.list[cItem.y/this.cellHeight][cItem.x/this.cellWidth] = null;
							//console.log("del", cItem.x/this.cellWidth, cItem.y/this.cellHeight);
						//}else{
							//dlist.push(cItem);
							//this.removeChild(cItem);
							//this.list[this.selItem.y/this.cellHeight][this.selItem.x/this.cellWidth] = null;
							//console.log("del", this.selItem.x/this.cellWidth, this.selItem.y/this.cellHeight);
						//}
						for(let i=0;i<dlist.length;i++){
							console.log("delx", dlist[i].x/this.cellWidth, dlist[i].y/this.cellHeight);
							this.removeChild(dlist[i]);
							this.list[dlist[i].y/this.cellHeight][dlist[i].x/this.cellWidth] = null;
						}
						this.selItem = null;
						this.selMask.visible = false;
						this.reShow();
					}, this);
					return;
				}
			}while(false);

			this.selItem = e.target;
			this.selMask.visible = true;
			this.selMask.x = this.selItem.x;
			this.selMask.y = this.selItem.y;
		}
		/** 检查4方向的元素是否相同 */
		private checkItem(dl, dm, wi:cellM, ci:cellM):void{
			let cx = wi.x / this.cellWidth, cy = wi.y / this.cellHeight;
			let cv = [[0, 1], [0, -1], [1, 0], [-1, 0]];
			for(let i=0;i<cv.length;i++){
				let nx = cx + cv[i][0], ny = cy + cv[i][1];
				if(nx < 0 || ny < 0 || nx >= this.Xnum || ny >= this.Ynum) continue;
				if(this.list[ny][nx] == null || this.list[ny][nx].texture != ci.texture) continue;
				if((nx + "_" + ny) in dm) continue;
				dl.push(this.list[ny][nx]);
				dm[nx + "_" + ny] = 1;
			}
		}
		/** 刷新显示的元素 */
		private reShow():void{
			for(let i=0;i<this.Xnum;i++){
				let cj = this.Ynum-1;
				for(;cj>-1;cj--){ //取最后一个非空元素
					if(this.list[cj][i] == null) break;
				}
				if(cj > -1) console.log("move", i, cj);
				for(let j=cj-1;j>-1;j--){
					if(this.list[j][i]){
						this.list[cj][i] = this.list[j][i];
						this.list[j][i] = null;
						egret.Tween.get(this.list[cj][i]).to({y:cj*this.cellHeight}, 500);
						cj--;
					}
				}
			}
		}
	}

	/** 每个元素的类 */
	class cellT extends egret.TextField {

	}

	/** 每个元素的类 */
	class cellM extends egret.Bitmap {

	}
}